// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {

        enemyPlane: {
            default: null,
            type: cc.Prefab
        },

        Mystart: {
            default: null,
            type: cc.Prefab
        }

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {

        this.newX = 0;
        this.newY = 0;
        this.i = 0;
        cc.director.getPhysicsManager().enabled = true;//开启物理系统
    },

    start() {

    },

    spawnNewStar() {
        //var newX = (Math.random() - 0.5) * this.node.width;
        //var newY = Math.random() * this.node.height + this.node.y;

        var newPlane = cc.instantiate(this.enemyPlane);
        //newPlane.node.x = this.newX;
        //newPlane.node.y = this.newY;
        this.node.addChild(newPlane);

        newPlane.setPosition(this.getNewStarPosition());

        var rgb = newPlane.getComponent(cc.RigidBody);
        rgb.applyLinearImpulse(cc.v2(0, -200), rgb.getWorldCenter());

        newPlane.getComponent('enemytiny').prentNode = this;//定义prentNode等于this
    },

    newStar(pos) {
        var newStart = cc.instantiate(this.Mystart);
        this.node.addChild(newStart);

        newStart.setPosition(pos);
    },
    getNewStarPosition() {
        this.newX = (Math.random() - 0.5) * this.node.width;
        this.newY = Math.random() * this.node.height;

        return cc.v2(this.newX, this.newY);
    },

    update(dt) {
        this.i++;
        if (this.i >= 50) {
            this.spawnNewStar();
            this.i = 0;
        }
    },
});
